package com.jmga.ecogasfinder.activities.gasstation;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Date;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.jmga.ecogasfinder.R;
import com.jmga.ecogasfinder.activities.Preferences;
import com.jmga.ecogasfinder.providers.GasFinderProvider;
import com.jmga.ecogasfinder.vo.GasStation;

public class GasStationFavouritesActivity extends ListActivity {
	 ListView favouriteListView;
	 Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(getClass().getName(), "GasStationFavouritesActivity onCreate");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);		 
		setContentView(R.layout.gasstation_favourites);
		
		String whereFavourite = GasFinderProvider.KEY_FAVOURITE + " = 1";
		cursor = getContentResolver().query(GasFinderProvider.CONTENT_URI_STATIONS, null, whereFavourite, null, null); 
	    startManagingCursor(cursor); 
	    
        ListAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.two_line_list_item, cursor,
                new String[] {GasFinderProvider.KEY_ADDRESS, GasFinderProvider.KEY_LABEL},
                new int[] {android.R.id.text1, android.R.id.text2});

        setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	       
    	String labelString = null, operatorString = null, addressString = null, laneString = null, postalCodeString = null, provinceString = null,
    		localityString = null, saleTypeString = null, sentTypeString = null, xCoordString = null, yCoordString = null;
    	Date fecha = null;
    	float price = 0f;
        
        cursor.moveToPosition(position);
        labelString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_LABEL));
        operatorString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_OPERATOR));
        addressString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_ADDRESS));
        laneString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_LANE));
        postalCodeString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_POSTAL_CODE));
        provinceString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_PROVINCE));
        localityString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_LOCALITY));
        saleTypeString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_SALE_TYPE));
        sentTypeString = cursor.getString(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_SENT_TYPE));
        double xCoord = cursor.getDouble(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_X_COORD));
        double yCoord = cursor.getDouble(cursor.getColumnIndexOrThrow(GasFinderProvider.KEY_Y_COORD));
        xCoordString = String.valueOf(xCoord);
        yCoordString = String.valueOf(yCoord);
        
        String referencia = xCoordString + yCoordString;
        
        //finding the last price and date from price table
        Context context = getApplicationContext();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		int gasType = Integer.parseInt(prefs.getString(Preferences.PREF_GAS_TYPE, "1"));
        
        String where = GasFinderProvider.KEY_STATION + " = '" + referencia + "' AND " + 
        	GasFinderProvider.KEY_GAS_TYPE + " = '" + gasType + "'";
        String priceOrder = GasFinderProvider.KEY_PRICE + " DESC";
        Cursor cursorPrice = getContentResolver().query(GasFinderProvider.CONTENT_URI_PRICES, null, where, null, priceOrder);
        
        DecimalFormat df = new DecimalFormat();
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        symbols.setGroupingSeparator(' ');
        df.setDecimalFormatSymbols(symbols);
        
        if(cursorPrice.getCount() != 0) {
        	cursorPrice.moveToNext();
			String priceValueString = cursorPrice.getString(3);
			
			double priceValue;
			try {
				priceValue = (Double) df.parse(priceValueString);
			} catch (ParseException e) {
				priceValue = 0d;
				Log.d(getClass().getName(), "Exception caughted " + e.getLocalizedMessage());
			}
			fecha = new Date(Long.parseLong(cursorPrice.getString(1)));
			price = (float) priceValue;
        } else {
        	fecha = new Date();
        }
        
  		Intent intent = new Intent(context, GasStationTabsViewActivity.class);
		GasStation gasStation = new GasStation(labelString, operatorString, addressString,
				laneString,	postalCodeString, provinceString, localityString, fecha,
				saleTypeString,	sentTypeString, xCoordString, yCoordString, referencia, price);
		intent.putExtra(GasFinderProvider.KEY_STATION, gasStation);  		
  		startActivity(intent);
	}
	
	@Override
	protected void onDestroy() {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onDestroy()");
		super.onDestroy();
		
	}

	@Override
	protected void onPause() {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onPause()");
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onRestart()");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onResume()");
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onStart()");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onStop()");
		super.onStop();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onRestoreInstanceState()");
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.d(getClass().getName(), "GasStationFavouritesActivity super.onRestoreInstanceState()");
		super.onSaveInstanceState(outState);
	}

}